package com.carpool.message;

import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.google.android.maps.GeoPoint;

public class Estimate {
	private static final String BASE_URL = "http://maps.google.com/maps?f=d&hl=en";
	private static final String ELEMENT_DESC = "description";
	private static final String ELEMENT_PLACEMARK = "Placemark";
	private static double PricePerKm = 0.5;
	String amount;
	String distance;
	private GeoPoint endPoint;
	private GeoPoint startPoint;
	String time;

	public Estimate(GeoPoint startPoint, GeoPoint endPoint) {
		this.startPoint = startPoint;
		this.endPoint = endPoint;
		distance = null;
		time = null;
		amount = null;
		try {
			initial();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public String estimateAmount() {
		amount=Double.toString(Double.parseDouble(distance)*PricePerKm);
		return amount;
	}
	public String estimateTime() {
		return time;
	}
	public String estimateDistance() {

		return distance;
	}

	private void initial() throws Exception {
		StringBuilder urlString = new StringBuilder();
		urlString.append(BASE_URL).append("&saddr=")
				.append(startPoint.getLatitudeE6() / 1E6).append(",")
				.append(startPoint.getLongitudeE6() / 1E6).append("&daddr=")
				.append(endPoint.getLatitudeE6() / 1E6).append(",")
				.append(endPoint.getLongitudeE6() / 1E6)
				.append("&ie=UTF8&0&om=0&output=kml");
		try {
			URL url = new URL(urlString.toString());
			HttpURLConnection connection = (HttpURLConnection) url
					.openConnection();
			connection.setRequestMethod("GET");
			connection.setDoOutput(true);
			connection.setDoInput(true);
			connection.connect();
			distance = parseResponse(connection.getInputStream());
		} catch (Exception e) {
			// Don't handle the exception but set the Route to null.
			//
			distance = null;
		}
	}

	private String parseResponse(InputStream inputStream)
			throws ParserConfigurationException, SAXException, IOException {
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		DocumentBuilder builder = factory.newDocumentBuilder();
		Document document = builder.parse(inputStream);
		NodeList placemarkList = document
				.getElementsByTagName(ELEMENT_PLACEMARK);
		NodeList children = placemarkList.item(placemarkList.getLength() - 1)
				.getChildNodes();

		for (int j = 0; j < children.getLength(); j++) {
			Node node = children.item(j);
			if (node.getNodeName().equals(ELEMENT_DESC)) {
				String temp = node.getFirstChild().getNodeValue();
				temp = temp.split("<br/>")[0].substring(10).replace("&#160;",
						" ");
				distance = temp.split(" ")[0];
				time = temp.split("km")[1];
			}
		}

		return distance;
	}

}
